<template>
  <div  style="height: 100vh;background-image: linear-gradient(135deg,#1352F2,#3C82F5)">
    <el-container style="padding: 30px">
      <el-header>
        <el-row>
          <el-col :span="4">
            <img src="/imgs/login/logo.png" style="height: 50px">
          </el-col>
          <el-col :span="1">
            <img src="/imgs/login/auto.png" style="height: 50px">
          </el-col>
          <el-col :span="1">
            <img src="/imgs/login/verticalLine.png" style="height: 50px">
          </el-col>
          <el-col :span="18" style="text-align: left">
            <span style="color:#fff;font-size: 30px;font-weight: bold">智慧车辆运营管理系统</span>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <el-row>
          <el-col :span="16">
           <!--     图片       -->
            <img src="/imgs/login/bg.png" style="width: 90%;margin-top: 50px">
          </el-col>
          <el-col :span="8">
            <!--    表单显示在一个卡片上        -->
            <el-card style="max-width: 480px">
              <h3 style="text-align: center">用户登录</h3>
              <!--     登录表单         -->
              <el-form label-position="top">
                <el-form-item label="手机号" >
                  <el-input type="text" placeholder="请输入手机号" v-model="user.username"/>
                </el-form-item>
                <el-form-item label="密码" >
                  <el-input type="password"  placeholder="请输入密码" v-model="user.password" @keydown.enter="login()"/>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="width: 100%" @click="login()">
                    登录
                  </el-button>
                </el-form-item>
              </el-form>
            </el-card>
          </el-col>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>

import {ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
import router from "@/router";
//声明登录的user对象
const user = ref({
  username:'',
  password:''
})
//登录方法
const login = ()=>{
  console.log(user)
  //给后端发送请求，利用的是axios
  //get请求还是post请求,post请求！
  //url  就是访问后端登录的路径： http://localhost:8080/v1/user/login
  //利用qs将js对象转成字符串， username=admin&password=admin
  let data = qs.stringify(user.value)
  axios.post(BASE_URL+'/login',data).then(
      response=>{
      //现在控制台打印一下
      console.log(response)
      if(response.data.code == 2000){
        //给出登录成功的提示 ElMessage实现，能在页面顶部提示一个登录成功的标识，3秒后消失
        ElMessage.success('登陆成功')
        //将登录成功的用户信息存储到localStorage中,字符串键值对的形式持久化存储
        //JSON.stringify(user)可以将js对象转成JSON格式的字符串存储
        //localStorage.setItem('user',JSON.stringify(user))
        localStorage.user=JSON.stringify(response.data.data)
        //登录成功后跳转到首页 /user
        router.push('/user')
      } else {
        ElMessage.error(response.data.msg)
      }
  }
  )
}
</script>

<style scoped>

</style>